package vp.remedies.dr.home.allmain.remedy.activity;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.adapter.SearchableAdapter;
import vp.remedies.dr.home.db.AndroidSQLiteHelper;
import vp.remedies.dr.home.so.NutrientPropertiesDetails;
import vp.remedies.dr.home.so.NutrientPropertiesWrapper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class IncreaseImmunePowerActivity extends Activity {

	SearchableAdapter searchableAdapter;
	public static NutrientPropertiesWrapper nutrientPropertiesWrapper;
	// List view
	private ListView listView;

	// Search EditText
	EditText inputSearch;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nutrient_properties_list);

		inputSearch = (EditText) findViewById(R.id.inputSearch);
		listView = (ListView) findViewById(R.id.nutrient_properties_list);

		nutrientPropertiesWrapper = getNutrientPropertiesWrapper();

		searchableAdapter = new SearchableAdapter(this,
				nutrientPropertiesWrapper);

		listView.setAdapter(searchableAdapter);

		/**
		 * Enabling Search Filter
		 * */
		inputSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				// When user changed the Text
				IncreaseImmunePowerActivity.this.searchableAdapter.getFilter()
						.filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
			}
		});

		listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int arg2, long arg3) {
				// On selecting single track get song information
				Intent i = new Intent(view.getContext(),
						NutrientsPropertiesActivity.class);
				TextView textView = (TextView) view
						.findViewById(R.id.foodNameTxt);
				Log.i("test", "textView::" + textView.getText());
				String foodNameTxt = (String) textView.getText();
				Log.i("test", "foodNameTxt::" + foodNameTxt);
				NutrientPropertiesDetails nutrientPropertiesDetails = nutrientPropertiesWrapper
						.getNutrientPropertiesMap().get(foodNameTxt);
				Toast.makeText(view.getContext(),
						nutrientPropertiesDetails.foodName, Toast.LENGTH_LONG)
						.show();

				i.putExtra(NutrientsPropertiesActivity.FOOD_NAME,
						nutrientPropertiesDetails.foodName);
				i.putExtra(NutrientsPropertiesActivity.GOOD_FOR,
						nutrientPropertiesDetails.goodFor);
				i.putExtra(NutrientsPropertiesActivity.PROPERTIES,
						nutrientPropertiesDetails.properties);
				i.putExtra(NutrientsPropertiesActivity.FOOD_IMAGE_NAME,
						nutrientPropertiesDetails.imageName);
				i.putExtra(NutrientsPropertiesActivity.NUTRIENTS,
						nutrientPropertiesDetails.nutrients);
				startActivity(i);

			}
		});
	}

	public static NutrientPropertiesWrapper getNutrientPropertiesWrapper() {
		nutrientPropertiesWrapper = new NutrientPropertiesWrapper();
		AndroidSQLiteHelper helper = AndroidSQLiteHelper.getInstance();
		Cursor cursor = helper
				.getCursorRaw(
						"SELECT  id , food_name , properties, nutrients, good_for, bad_for, image_name , fav FROM nurtrients_properties_en ",
						null);

		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String food_name = cursor.getString(1);
			String properties = cursor.getString(2);
			String nutrients = cursor.getString(3);
			String good_for = cursor.getString(4);
			String bad_for = cursor.getString(5);
			String image_dame = cursor.getString(6);
			String favorite = cursor.getString(7);
			nutrientPropertiesWrapper.getIds().add(id);
			nutrientPropertiesWrapper.getFoodNameList().add(food_name);
			nutrientPropertiesWrapper.getGoodForList().add(good_for);
			NutrientPropertiesDetails nutrientPropertiesDetails = new NutrientPropertiesDetails();
			nutrientPropertiesDetails.foodName = food_name;
			nutrientPropertiesDetails.properties = properties;
			nutrientPropertiesDetails.nutrients = nutrients;
			nutrientPropertiesDetails.goodFor = good_for;
			nutrientPropertiesDetails.badFor = bad_for;
			nutrientPropertiesDetails.imageName = image_dame;
			nutrientPropertiesDetails.favorite = favorite;
			nutrientPropertiesWrapper.getNutrientPropertiesMap().put(food_name,
					nutrientPropertiesDetails);
		}
		return nutrientPropertiesWrapper;
	}
}